# coding=utf8
import sys
import re
if sys.platform == 'win32':
    from twisted.internet import win32eventreactor
    win32eventreactor.install()
from twisted.internet.serialport import SerialPort
from twisted.internet import protocol,task, reactor, error
from twisted.protocols.basic import LineReceiver
class GasSim(LineReceiver):
    def __init__(self):
        self._lc1 = task.LoopingCall(self.sendCmd)
        self._lc1.start(3,False)
    def sendCmd(self):
        self.sendLine("@01c100001")
    def lineReceived(self, line):
        print(line)
        if(re.match('@\d{2}\D\w{4}\d{2}',line)):
            if(line[3] == 'D'):
                print u'接收到1条命令！\r\n'
                tmpstr = "当前电压值=%.3f" % (int(line[4:8],16)/1000.0)
                print tmpstr
if __name__ == '__main__':
    s = SerialPort(GasSim(), 'COM2', reactor, baudrate=9600)
    reactor.run()